package com.lizi.db;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lizi.core.util.ExcelTools;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class Main {

	public static void main(String[] args) {
		List<Map<String, Object>> data =  query();
		ExcelTools tool = new ExcelTools();
		String[] headers = 
				new String[]{"商品标题","副标题","商品url","商品介绍","商品描述","品牌介绍"};
		String[] keys = 
				new String[]{"title","description","url","itemDetail","itemDiscription","brandDiscription"};
		tool.saveExcel("item","E:\\","item.xlsx",headers, keys, data);
	}
	 
	
	public static List<Map<String, Object>> query() {

		DBObject condition = new BasicDBObject();
		condition.put("status", "FOR_SALE");

		DBCollection commentDB = DbUtils.getDB().getCollection("item");
		DBCursor cursor = commentDB.find(condition);
		
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map ;
		while(cursor.hasNext()){
			map =  new HashMap<>(); 
			DBObject dbObject = cursor.next();
			map.put("title", dbObject.get("title"));
			map.put("description", dbObject.get("description"));
			map.put("url", MessageFormat.format("http://www.lizi.com/product-{0}.html", dbObject.get("itemNum")) );
			
			List<Map<String, Object>> itemDetailList = (List<Map<String, Object>>) dbObject.get("itemDetailList");
			if(itemDetailList != null){
				for(Map<String, Object> m :itemDetailList){
					if(m.get("detailName").equals("商品介绍")){
						map.put("itemDetail", m.get("detailContent"));
					}
					if(m.get("detailName").equals("商品描述")){
						map.put("itemDiscription", m.get("detailContent"));
					}
				}
			}
			

			map.put("brandDiscription", "");
			list.add(map);
		}
		
		return list;
	}
}
